package com.blog.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.blog.pojo.Blog;
import com.blog.pojo.Comment;
import com.blog.pojo.User;
import com.blog.service.BlogService;
import com.blog.service.CommentService;
import com.blog.service.UserService;
import com.blog.util.ErrorMsg;
/**
 * 添加评论
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {

	@Resource
	private UserService userService;
	
	/**
	 * 用户注册
	 * comment 评论参数
	 * imageCode 验证码
	 * @return
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Model add(User user,/*String imageCode,*/Model model,HttpServletRequest request,HttpSession httpSession){
		//获取系统生成的评论验证码
	//	String sRend = (String) httpSession.getAttribute("sRend");
		//判断验证码是否正确
	/*	if(!imageCode.equals(sRend)){
			ErrorMsg.successFalse(model, ErrorMsg.iamgeCode);
			return model;
		}else{*/
		//前端加密后提交
			int isok = userService.add(user);
			if(isok > 0){
				// 成功后该博客的回复次数加1
				model.addAttribute("success", true);
			}else{
				ErrorMsg.successFalse(model, "注册失败");
			}
	//	}
		return model;
	}

	@RequestMapping("/loginUser")
	public ModelAndView loginUser(String userName,String password){
		ModelAndView mav = new ModelAndView();
		User user = userService.findByUserName(userName, password);
		if(user == null){
			mav.addObject("success", false);
			mav.addObject("errorInfo","用户名或密码错误");
		}else{
			mav.addObject("success", true);
			mav.addObject("user", user);
		}
		return mav;
	}
	
}
